package com.stn.zjx.givingtreasure.net.impl;

import android.annotation.SuppressLint;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.stn.zjx.givingtreasure.net.PayApi;
import com.stn.zjx.givingtreasure.net.UserAddressApi;
import com.stn.zjx.givingtreasure.net.UserApi;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;
import com.vondear.rxtools.view.RxToast;

import org.json.JSONObject;

public class UserAddressNetImpl {
    /**
     * @apiNote 查询地址管理
     * @param token token
     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void query_user_address(Integer pageNow,Integer pageSize,String token,  String tag, StringCallback callback){

        OkGo.<String>post(UserAddressApi.query_user_address)
                .tag(tag)
                .cacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                .cacheKey(tag)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .isSpliceUrl(true)
                .upJson("")
                .params("pageNow",pageNow)
                .params("pageSize",pageSize)
                .params("token",token)
                .execute(callback);
    }


    /**
     * @apiNote 新增地址
     * @param token token
     * @param userAddressName 真实姓名
     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void add_address(String token,
                                         String userAddressName,
                                         String userAddressPhone,
                                         String userAddressProvinceCityArea,
                                         String userAddressAddress,
                                         String userAddressTag,
                                          String userAddressExtra,
                                         Integer addressIsDefault,
            String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("userAddressName",userAddressName);
            jsonObject.put("userAddressPhone",userAddressPhone);
            jsonObject.put("userAddressProvinceCityArea",userAddressProvinceCityArea);
            jsonObject.put("userAddressAddress",userAddressAddress);
            jsonObject.put("userAddressTag",userAddressTag);
            jsonObject.put("userAddressExtra",userAddressExtra);
            jsonObject.put("addressIsDefault",addressIsDefault);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(UserAddressApi.add_address)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("token",token)
                .params("data",data)
                .execute(callback);
    }


    /**
     * @apiNote 更新地址数据
     * @param token token
     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void update_address(String token,
                                   Integer userAddressId,
                                   String userAddressName,
                                   String userAddressPhone,
                                   String userAddressProvinceCityArea,
                                   String userAddressAddress,
                                   String userAddressTag,
                                   String userAddressExtra,
                                   Integer addressIsDefault,
                                   String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("userAddressId",userAddressId);
            jsonObject.put("userAddressName",userAddressName);
            jsonObject.put("userAddressPhone",userAddressPhone);
            jsonObject.put("userAddressProvinceCityArea",userAddressProvinceCityArea);
            jsonObject.put("userAddressAddress",userAddressAddress);
            jsonObject.put("userAddressTag",userAddressTag);
            jsonObject.put("userAddressExtra",userAddressExtra);
            jsonObject.put("addressIsDefault",addressIsDefault);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(UserAddressApi.update_address)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("token",token)
                .params("data",data)
                .execute(callback);
    }

    /**
     * @apiNote 删除地址数据
     * @param token token
     * @param tag 标签
     * @param callback 请求回调
     */
    @SuppressLint("CheckResult")
    public static void delete_address(String token,
                                      Integer userAddressId,
                                      String tag, StringCallback callback){
        String data="";
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("userAddressId",userAddressId);
            data= ToolsUtils.getEncryptionParams(jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();

        }
        OkGo.<String>post(UserAddressApi.delete_address)
                .tag(tag)
                .isSpliceUrl(true)
                .upJson("")
                .params("token",token)
                .params("data",data)
                .execute(callback);
    }

}
